//
//  CreateFoodViewController.m
//  ZooNgonDoiOS
//
//  Created by Trung sound on 6/5/12.
//  Copyright (c) 2012 Personal. All rights reserved.
//

#import "CreateFoodViewController.h"
#import "CustomAlertView.h"

@implementation CreateFoodViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
        [lbl setText:@"Create food Screen"];
        [self.view addSubview:lbl];
        UIView *createFoodView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
        createFoodView.backgroundColor = [UIColor grayColor];
        
        UIButton *btnTakePicture = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btnTakePicture.frame = CGRectMake(50, 50, 100, 50);
        [btnTakePicture setTitle:@"Take Picture" forState:UIControlStateNormal];
        [btnTakePicture addTarget:self action:@selector(btnTakePictureClicked) forControlEvents:UIControlEventTouchUpInside];
        [createFoodView addSubview:btnTakePicture];
        
        _imageFood = [[UIImage alloc]init];
        _vImage = [[UIImageView alloc]initWithImage:_imageFood];
        _vImage.frame = CGRectMake(0, 150, 320, 320);
//        _vImage.backgroundColor = [UIColor whiteColor];
        _vImage.backgroundColor = [UIColor colorWithRed:127 green:127 blue:127 alpha:0.5];
        _imagePicker = [[UIImagePickerController alloc]init];
        _imagePicker.allowsEditing = YES;
        
        _imagePicker.delegate = self;
        
        [createFoodView addSubview:_vImage];
        self.view = createFoodView;
        
    }
    return self;
}

- (void) btnTakePictureClicked {
    
    if ([UIImagePickerController
         isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
         [self showAlertView];
    } else {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Camera not found" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
        _imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentModalViewController:_imagePicker animated:YES];
    }
}

- (void) showAlertView {
    UIAlertView *customAlertView = [[UIAlertView alloc]initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"Hủy" otherButtonTitles:@"OPEN CAMERA", @"FROM GALLERY", nil];

//    CustomAlertView *customAlertView = [[CustomAlertView alloc]initWithFrame:CGRectMake(25, 75, 265, 324)];
    [customAlertView show];
    [customAlertView release];
//    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"CANCEL" otherButtonTitles:@"OPEN CAMERA", @"FROM GALLERY", nil];
//    [alertView show];
//    [alertView release];
}
    
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        UIButton *btnPick = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btnPick.frame = CGRectMake(280, 5, 30, 20);
        [btnPick setTitle:@"Open" forState:UIControlStateNormal];
        UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
                                                                        style:UIBarButtonItemStyleDone target:nil action:nil];
        _imagePicker.navigationItem.rightBarButtonItem = rightButton;
        _imagePicker.navigationBarHidden = YES;
        _imagePicker.navigationBar.tintColor = [UIColor orangeColor];
        _imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;

        [self presentModalViewController:_imagePicker animated:YES];
    } else if (buttonIndex == 2) {
        _imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//        _imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        [self presentModalViewController:_imagePicker animated:YES];
    } 
}


#pragma mark - Image picker delegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    
    _vImage.image = image;
    [picker dismissModalViewControllerAnimated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSLog(@"Media info: %@", info);
    
    if (_imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {
            
        //obtaining saving path
        
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSLog(@"%@",paths);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"../latest_photo.png"];
        NSLog(@"%@",imagePath);
        
            //extracting image from the picker and saving it
        NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];   
        if ([mediaType isEqualToString:@"public.image"]){
            UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];

            NSData *webData = UIImagePNGRepresentation(editedImage);
            [webData writeToFile:imagePath atomically:YES];
            
            UIImageWriteToSavedPhotosAlbum(editedImage, self, nil, nil);

            _vImage.image = editedImage;
            [self dismissModalViewControllerAnimated:YES];
        }
        
    } else if (_imagePicker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
        
        NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];   
        if ([mediaType isEqualToString:@"public.image"]){
            
  
            UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage]; 
            _vImage.image = editedImage;
            
            [self dismissModalViewControllerAnimated:YES];
        }
    }
    
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewWillAppear:(BOOL)animated {
    UITabBarItem *tabBarItem = [[UITabBarItem alloc]initWithTitle:@"ABC" image:[UIImage imageNamed:IMG_BTN_HOME_NORMAL] tag:1];
    
    self.tabBarItem.image = [UIImage imageNamed:IMG_BTN_HOME_NORMAL];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
